package com.example.pages;

import com.example.model.SortedListOf;
import com.example.model.User;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;

public class UserPage extends InternalPage{

    @FindBy(name = "username")
    public WebElement usernameField;

    @FindBy(name = "email")
    public WebElement emailField;

    @FindBy(name = "password")
    public WebElement passwordField;

    @FindBy(name = "password2")
    public WebElement passwordConfirmationField;

    @FindBy(name = "submit")
    public WebElement saveButton;

    @FindBy(xpath = "//h2")
    public WebElement titlePageUserManagement;

    @FindBy(xpath = "//tr[2]/td[2]/a")
    public WebElement newUser;
    
    @FindBy(css = "#user-list>table>tbody>tr")
    public List<WebElement> userList;

    private SortedListOf<User> oldUserList;
    private SortedListOf<User> newUserList;
    private User user;

    public SortedListOf<User> getOldUserList(){
        return oldUserList;
    }

    public SortedListOf<User> getNewUserList(){
        return newUserList;
    }

    public User getUser(){
        return user;
    }

    public UserPage sendFormNewUser(User user) {
        usernameField.sendKeys(user.name);
        emailField.sendKeys(user.email);
        passwordField.sendKeys(user.password);
        passwordConfirmationField.sendKeys(user.password);
        saveButton.click();
        cacheedUsers = null;
        return MyPageFactory.getPage(driver, UserPage.class);
    }

    public UserPage deleteUser(User user) {
        for(WebElement row : userList){
            if(user.equals(convertRowToUser(row))){
                row.findElement(By.xpath("./td[6]/a")).click();
                waitEvent().until(alertIsPresent()).accept();
                break;
            }
        }
        cacheedUsers = null;
        return MyPageFactory.getPage(driver, UserPage.class);
    }

    private SortedListOf<User> cacheedUsers;

    public SortedListOf<User> getUsers() {
        if(cacheedUsers == null){
            cacheedUsers = new SortedListOf<User>();
            for(WebElement row : userList){
                cacheedUsers.add(convertRowToUser(row));
            }
        }
        return new SortedListOf<User>(cacheedUsers);
    }

    public int getUserCount(){
        return userList.size();
    }

    private User convertRowToUser(WebElement row) {
        List<WebElement> cells = row.findElements(By.tagName("td"));
        return new User()
                .withName(cells.get(1).getText())
                .withEmail(cells.get(2).getText());
    }

    public UserPage createNewUsers(String username, String email, String password){
        oldUserList = getUsers();
        user = new User()
                .withName(username).withEmail(email).withPassword(password);
        if(oldUserList.contains(user)){
            deleteUser(user);
            oldUserList = getUsers();
        }
        sendFormNewUser(user);
        newUserList = getUsers();
        return MyPageFactory.getPage(driver, UserPage.class);
    }

    public UserPage deleteUserNotRoleAdmin(){
        oldUserList = getUsers();
        if(getUserCount() == 1){
            createNewUsers("user", "user@mail.com", "password")
                .deleteUserNotRoleAdmin();
            return MyPageFactory.getPage(driver, UserPage.class);
        } else{
            for(WebElement row: userList){
                String role = row.findElement(By.xpath("./td[4]")).getText();
                if(!role.contains("Admin")){
                    row.findElement(By.xpath("./td[6]/a")).click();
                    waitEvent().until(alertIsPresent()).accept();
                    break;
                }
            }
            newUserList = getUsers();
            cacheedUsers = null;
            return MyPageFactory.getPage(driver, UserPage.class);
        }
    }
}
